package com.itheima.bos.web.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.bos.constant.Constants;
import com.itheima.bos.domain.page.PageBean;
import com.itheima.bos.domain.take_delivery.Promotion;
import com.itheima.bos.web.action.common.BaseAction;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

@ParentPackage("json-default")
@Namespace("/promotion")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion> {

	@Action(value = "promotion_pageQuery", results = { @Result(name = "success", type = "json") })
	public String promotion_pageQuery() {
		PageBean<Promotion> pageBean = WebClient.create(
				Constants.BOS_MANAGEMENT_URL + "/services/promotionService/pageQuery?page=" + page + "&rows=" + rows)
				.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).get(PageBean.class);
		ServletActionContext.getContext().getValueStack().push(pageBean);
		return SUCCESS;
	}

	@Action(value = "promotion_detail")
	public String promotion_detail() {
		// 获取绝对路径
		String realPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
		File promotion_detail_file_path = new File(realPath + "/" + model.getId() + ".html");
		if (!promotion_detail_file_path.exists()) {
			try {
				Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
				configuration.setDirectoryForTemplateLoading(
						new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/template")));
				Template template = configuration.getTemplate("promotion_detail.ftl");
				Promotion promotion = WebClient
						.create(Constants.BOS_MANAGEMENT_URL + "/services/promotionService/findById?id=" + model.getId())
						.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).get(Promotion.class);
				HashMap<String, Object> hashMap = new HashMap<>();
				hashMap.put("promotion", promotion);
				template.process(hashMap, new PrintWriter(new FileOutputStream(promotion_detail_file_path)));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		try {
			FileUtils.copyFile(promotion_detail_file_path,ServletActionContext.getResponse().getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return NONE;
	}
}
